import 'dart:async';
import 'package:flutter/material.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBCommonToast/YBCommonToastPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';


class YBSettingYanZhengPhonePage extends StatefulWidget {

  String phone;
  YBSettingYanZhengPhonePage({this.phone});

  @override
  _YBSettingYanZhengPhonePageState createState() => _YBSettingYanZhengPhonePageState(phoneStr: phone);
}

class _YBSettingYanZhengPhonePageState extends State<YBSettingYanZhengPhonePage> {

  final String phoneStr;
  String msgTokenStr;
  Timer _timer;
  int _timeCount = 60;
  String _autoText;

  TextEditingController _editingController = TextEditingController();

  _YBSettingYanZhengPhonePageState({this.phoneStr});

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Scaffold(
         appBar: AppBar(
           elevation: 0,
           title: Text(
             YBChineseText.mineXiuGaiPhoneNumberTitle,
             style: YBTextStyle.TextStyle30313320Medium,
           ),
         ),
         body: SingleChildScrollView(
           child: Padding(
             padding:EdgeInsets.fromLTRB(YBTextStyle.adapterSize20, YBTextStyle.adapterSize30, YBTextStyle.adapterSize20,0),
             child: Container(
               color: YBColor.white,
               child: Column(
                 mainAxisAlignment: MainAxisAlignment.start,
                 crossAxisAlignment: CrossAxisAlignment.stretch,
                 children: [
                   Container(
                     alignment: Alignment.center,
                     child: Text(YBChineseText.mineCurrentBindingPhoneTitle,style: YBTextStyle.TextStyle60626611,),
                   ),
                   Container(
                     height: 5,
                   ),
                   Container(
                     alignment: Alignment.center,
                     child: Text(phoneStr??"",style: YBTextStyle.TextStyle18181820Medium,),
                   ),
                   Container(
                     height: 40,
                   ),
                   Container(
                     alignment: Alignment.center,
                     child: Text(YBChineseText.mineYanZhengShengFengTitle,style: YBTextStyle.TextStyle1B1C1F18Medium,),
                   ),
                   Container(
                     height: 30,
                   ),
                   Container(
                     height: 50,
                     child: Row(
                       children: [
                         Container(
                           child: Image.asset(YBImageStyle.getImagePath('login_dunphone')),
                         ),
                         Container(
                           width: 8,
                         ),
                         Expanded(
                           child: TextField(
                             decoration: InputDecoration(
                               hintText: YBChineseText.mineInputYanZhengMaTitle,
                               border: InputBorder.none,
                             ),
                             controller: _editingController,
                           ),
                         ),
                         Container(
                           child: RawMaterialButton(
                               child: Text(_autoText != null? _autoText:YBChineseText.mineGetYanZhengMaTitle,style: YBTextStyle.TextStyle2D69E014Medium,),
                               onPressed: _timeCount == 60?()async{
                                 _senderTimer();
                               }:null),
                         ),
                       ],
                     ),
                   ),
                   Container(
                     color: YBColor.colorF5F5F7,
                     height: 1,
                   ),
                   Container(
                     height: YBTextStyle.adapterSize30,
                   ),
                   Container(
                     height: 44,
                     child: RawMaterialButton(
                         child: Text(YBChineseText.workTaiNextStepTitle,style:YBTextStyle.TextStyleFFFFFF16Medium,),
                         fillColor: YBColor.commonThemeColor,
                         shape: RoundedRectangleBorder(
                           borderRadius: BorderRadius.circular(8.0),
                         ),
                         onPressed:(){

                           if (_editingController.text == null){
                             YBToast.showToastInfo(YBChineseText.mineYanZhengMaKongTitle);
                             return;
                           }
                           _changeYanZhengMaCode(msgTokenStr, phoneStr, _editingController.text);
                         }),
                   ),
                 ],
               ),
             ),
           ),
         ),
       ),
    );
  }

  //获取验证码
  void _initLogin(){
    YBLoginInfoApi.loginInMsgCode({"phone":phoneStr,'sysCode':"xg_youbang"},success:(data){
      print('----$data----');
      var msgTokenMap = data;
      //msgToken
      print('msgToken---$msgTokenMap');
      if (msgTokenMap['msgToken'].toString().isNotEmpty) {
        setState(() {
          msgTokenStr = msgTokenMap['msgToken'];
        });
      }
    },fail: (error){
      print("---$error---");
    });
  }

  //验证通过
  void _changeYanZhengMaCode(String msgToken,String phoneStr,String msgCode){
    YBMineNetWork.mineSettingMyChangeYanZhengMa({
      "sysCode":"xg_youbang",
      "msgToken":msgToken,
      "phone":phoneStr,
      "msgCode":msgCode,
    },success: (data){
      print('object-----$data');
      YBApplication.router.navigateTo(context, '/mine/mineSettingPage/mineSettingXiuGaiPhoneYanMaPage/mineSettingNewPhonePage?phoneN=$phoneStr');
    },fail: (error){
      print('object-----$error');
    });
  }

  void _senderTimer(){
    _initLogin();
    YBToast.showToastInfo("短信验证码已发送，请注意查收");
    _timer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
      setState(() {
        if (_timeCount <= 0){
          _timer.cancel();
          _timeCount = 60;
          _autoText = "重新获取";
        }else{
          _timeCount -= 1;
          _autoText = '$_timeCount' + "S";
        }
      });
    });
  }

}
